package com.jyx.ucenter.service;

import com.jyx.framework.domain.ucenter.JyxCompanyUser;
import com.jyx.framework.domain.ucenter.JyxMenu;
import com.jyx.framework.domain.ucenter.JyxUser;
import com.jyx.framework.domain.ucenter.ext.JyxUserExt;
import com.jyx.ucenter.dao.JyxCompanyUserRepository;
import com.jyx.ucenter.dao.JyxMenuMapper;
import com.jyx.ucenter.dao.JyxUserRepository;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService {
    @Autowired
    private JyxUserRepository jyxUserRepository;
    @Autowired
    private JyxCompanyUserRepository jyxCompanyUserRepository;
    @Autowired
    private JyxMenuMapper jyxMenuMapper;


    //查询用户名
    public JyxUser findJyxUserByUsername(String username){
        return jyxUserRepository.findJyxUserByUsername(username);
    }
    //根据账号查询用户的信息，返回用户扩展信息
    public JyxUserExt getUserExt(String username){
        JyxUser jyxUser = findJyxUserByUsername(username);
        if(jyxUser == null){
            return null;
        }
        String userId = jyxUser.getId();
        List<JyxMenu> menuList = jyxMenuMapper.selectPermissionByUserId(userId);
        JyxUserExt jyxUserExt = new JyxUserExt();
        BeanUtils.copyProperties(jyxUser,jyxUserExt);
        jyxUserExt.setPermissions(menuList);
        JyxCompanyUser jyxCompanyUser = jyxCompanyUserRepository.findByUserId(userId);
        if(jyxCompanyUser != null){
            String companyId = jyxCompanyUser.getCompanyId();
            jyxUserExt.setCompanyId(companyId);
        }
        return jyxUserExt;
    }

}
